CC = cc
CFLAGS = -Iinclude -Ilib/unity -Wall -Wextra -g -DDEBUG -DUSE_COLOR

# 源文件
SRC_UNITY = lib/unity/unity.c
SRC_CONSTANTS = src/constants.c
SRC_STUDENT_DATA = src/student_data.c
SRC_STUDENT_SERVICE = src/student_service.c
SRC_STUDENT_INTERFACE = src/student_interface.c
SRC_VIEW = src/view.c
SRC_STUDENT_VIEW = src/student_view.c
SRC_MAIN = src/main.c

SRC_TEST_LIST = tests/data_structure/test_list.c
SRC_TEST_STUDENT_DATA = tests/student/test_student_data.c
SRC_TEST_STUDENT_SERVICE = tests/student/test_student_service.c

# 对象文件
OBJ_UNITY = $(SRC_UNITY:.c=.o)
OBJ_CONSTANTS = $(SRC_CONSTANTS:.c=.o)
OBJ_STUDENT_DATA = $(SRC_STUDENT_DATA:.c=.o)
OBJ_STUDENT_SERVICE = $(SRC_STUDENT_SERVICE:.c=.o)
OBJ_STUDENT_INTERFACE = $(SRC_STUDENT_INTERFACE:.c=.o)
OBJ_VIEW = $(SRC_VIEW:.c=.o)
OBJ_STUDENT_VIEW = $(SRC_STUDENT_VIEW:.c=.o)
OBJ_MAIN = $(SRC_MAIN:.c=.o)

OBJ_TEST_LIST = $(OBJ_UNITY) $(SRC_TEST_LIST:.c=.o) $(OBJ_STUDENT_DATA)
OBJ_TEST_STUDENT_DATA = $(OBJ_UNITY) $(SRC_TEST_STUDENT_DATA:.c=.o) $(OBJ_STUDENT_DATA)
OBJ_TEST_STUDENT_SERVICE = $(OBJ_UNITY) $(SRC_TEST_STUDENT_SERVICE:.c=.o) $(OBJ_STUDENT_INTERFACE) $(OBJ_STUDENT_SERVICE) $(OBJ_STUDENT_DATA)

# 主程序目标
TARGET_MAIN = main_program

# 测试目标
TARGET_TEST_LIST = test_double_list
TARGET_TEST_STUDENT_DATA = test_student_data
TARGET_TEST_STUDENT_SERVICE = test_student_service

# 默认目标
all: $(TARGET_MAIN) $(TARGET_TEST_LIST) $(TARGET_TEST_STUDENT_DATA) $(TARGET_TEST_STUDENT_SERVICE)

# 编译源文件为对象文件
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@
	@printf "\n\n"

# 链接目标
$(TARGET_MAIN): $(OBJ_MAIN) $(OBJ_VIEW) $(OBJ_STUDENT_VIEW) $(OBJ_STUDENT_DATA) $(OBJ_STUDENT_SERVICE) $(OBJ_STUDENT_INTERFACE) $(OBJ_CONSTANTS)
	$(CC) $(OBJ_MAIN) $(OBJ_VIEW) $(OBJ_STUDENT_VIEW) $(OBJ_STUDENT_DATA) $(OBJ_STUDENT_SERVICE) $(OBJ_STUDENT_INTERFACE) $(OBJ_CONSTANTS) -o $(TARGET_MAIN)
	@printf "\n\n"

$(TARGET_TEST_LIST): $(OBJ_TEST_LIST)
	$(CC) $(OBJ_TEST_LIST) -o $(TARGET_TEST_LIST)
	@printf "\n\n"

$(TARGET_TEST_STUDENT_DATA): $(OBJ_TEST_STUDENT_DATA)
	$(CC) $(OBJ_TEST_STUDENT_DATA) -o $(TARGET_TEST_STUDENT_DATA)
	@printf "\n\n"

$(TARGET_TEST_STUDENT_SERVICE): $(OBJ_TEST_STUDENT_SERVICE)
	$(CC) $(OBJ_TEST_STUDENT_SERVICE) -o $(TARGET_TEST_STUDENT_SERVICE)
	@printf "\n\n"

# 清理构建生成的文件
clean:
	rm -f $(OBJ_UNITY) $(OBJ_STUDENT_DATA) $(OBJ_STUDENT_SERVICE) $(OBJ_VIEW)\
	      $(OBJ_STUDENT_INTERFACE) $(OBJ_STUDENT_VIEW)\
	      $(OBJ_MAIN) $(TARGET_MAIN) \
	      $(OBJ_TEST_LIST) $(TARGET_TEST_LIST) \
	      $(OBJ_TEST_STUDENT_DATA) $(TARGET_TEST_STUDENT_DATA) \
	      $(OBJ_TEST_STUDENT_SERVICE) $(TARGET_TEST_STUDENT_SERVICE)

	@printf "\n\n"

# 伪目标
.PHONY: all clean
